﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.AspNetCore.Mvc.Extensions;
using Abp.Domain.Uow;
using FwProject.Controllers;
using FwProject.Distribution;
using FwProject.Distribution.Dto;
using FwProject.PrePaymentInfo;
using FwProject.PrePaymentInfo.Dto;
using FwProject.Restaurant;
using FwProject.Settlement;
using FwProject.Settlement.Dto;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace FwProject.Web.Mvc.Controllers
{
    public class FinanceReportController : FwProjectControllerBase
    {
        private readonly ISettlementAccountAppService _settlementAccountAppService;
        private readonly IFinanceReportAppService _financeReportAppService;
        private readonly IDistributionAppService _distributionAppService;
        private readonly IHostingEnvironment _env;
        private readonly IRestaurantAppService _restaurantAppService;
        private readonly IDistributionLineAppService _distributionLineAppService;

        public FinanceReportController(ISettlementAccountAppService settlementAccountAppService, IDistributionAppService distributionAppService, IFinanceReportAppService financeReportAppService, IRestaurantAppService restaurantAppService, IDistributionLineAppService distributionLineAppService, IHostingEnvironment env)
        {
            _settlementAccountAppService = settlementAccountAppService;
            _distributionAppService = distributionAppService;
            _financeReportAppService = financeReportAppService;
            _restaurantAppService = restaurantAppService;
            _distributionLineAppService = distributionLineAppService;
            _env = env;
        }
        #region 资金池
        /// <summary>
        /// 资金池
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> CapitalPool(DateTime? date)
        {
            if (Request.IsAjaxRequest())
            {
                var model = await _settlementAccountAppService.GetCapitalPoolListAsync(date);
                return PartialView("CapitalPoolPartial", model);
            }
            return View();
        }

        public async Task<IActionResult> ExportCapitalPool(DateTime? date)
        {
            var list = await _settlementAccountAppService.GetCapitalPoolListAsync(date);
            return DoExcel<CapitalPoolDto>(list, _env);
        }
        #endregion




        /// <summary>
        /// 未收款客户一览表
        /// </summary>
        /// <param name="pageIdx"></param>
        /// <param name="RestaurantName"></param>
        /// <returns></returns>
        public async Task<IActionResult> NotReceivable(int pageIdx, int size, string RestaurantName)
        {
            if (pageIdx == 0)
                pageIdx = 1;
            if (size == 0)
                size = 20;

            var list = await _distributionAppService.GetNotReceivableListAsync(RestaurantName);
            ViewBag.SumTotalPrice = list.Sum(o => o.TotalPrice);
            ViewBag.SumReceivedPrice = list.Sum(o => o.ReceivedPrice);
            ViewBag.SumUnCollectedPrice = list.Sum(o => o.UncollectedPrice);

            var model = await _distributionAppService.GetNotReceivableRestaurantAsync(pageIdx, size, RestaurantName);
            if (Request.IsAjaxRequest())
            {
                return PartialView("NotReceivablePartial", model);
            }
            return View(model);
        }
        /// <summary>
        /// 未收款客户一览表---导出
        /// </summary>
        /// <param name="RestaurantName"></param>
        /// <returns></returns>
        public async Task<IActionResult> ExportNotReceivable(string RestaurantName)
        {
            var list = await _distributionAppService.GetNotReceivableListAsync(RestaurantName);
            return DoExcel<NotReceivableDto>(list, _env);
        }


        /// <summary>
        /// 财务汇总报表
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public async Task<IActionResult> FinanceSummary(DateTime? beginTime, DateTime? endTime)
        {
            if (Request.IsAjaxRequest())
            {
                var model = await _financeReportAppService.GetFinanceSummaryAsync(beginTime, endTime);
                return PartialView("FinanceSummaryPartial", model);
            }
            return View();
        }

        /// <summary>
        /// 结算账户周知明细
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> SettleAccountsKnowingDetail(int? id, int size, DateTime? beginTime, DateTime? endTime, Guid? accountId)
        {
            var pa = id ?? 1;

            if (size == 0)
                size = 20;

            var list = await _settlementAccountAppService.GetAllSettlementAccountAsync();
            ViewBag.Account = list.Select(o => new SelectListItem() { Value = o.Id.ToString(), Text = o.Name });

            if (Request.IsAjaxRequest())
            {
                var model = await _financeReportAppService.GetSettleAccountsKnowingDetailAsync(pa, size, beginTime, endTime, accountId);
                return PartialView("SettleAccountsKnowingDetailPartial", model);
            }

            return View();
        }

        public async Task<IActionResult> ExportExcelSettleAccountsKnowingDetail(DateTime? beginTime, DateTime? endTime, Guid? accountId)
        {
            var list = await _financeReportAppService.GetSettleAccountsKnowingDetailListAsync( beginTime, endTime, accountId);

            return DoExcel<SettleAccountsKnowingOutputDto>(list, _env);

        }

        #region 客户往来流水帐
        /// <summary>
        /// 客户往来流水账
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> RunningAccount(int pageIdx, int size, DateTime? beginTime, DateTime? endTime, string RestaurantName = "")
        {
            if (pageIdx == 0)
                pageIdx = 1;

            if (size == 0)
                size = 20;

            if (Request.IsAjaxRequest())
            {
                var model = await _financeReportAppService.GetRunningAccountPagedListAsync(pageIdx, size, beginTime, endTime, RestaurantName);
                return PartialView("RunningAccountPartial", model);
            }
            return View();
        }

        public async Task<IActionResult> ExportRunningAccount(DateTime? beginTime, DateTime? endTime, string RestaurantName = "")
        {
            var list = await _financeReportAppService.GetRunningAccountListAsync(beginTime, endTime, RestaurantName);
            return DoExcel<RunningAccountDto>(list, _env);

        }
        #endregion


        #region 应收对账单
        /// <summary>
        /// 应收对账单
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> ReceivableStatement(int? id, int size, DateTime? beginTime, DateTime? endTime, string restaurantIds = "")
        {
            var pa = id ?? 1;
            if (size == 0)
                size = 20;

            if (Request.IsAjaxRequest())
            {
                var model = await _financeReportAppService.GetReceivableStatementDataAsync(pa, size, beginTime, endTime, restaurantIds);
                return PartialView("ReceivableStatementPartial", model);
            }

            return View();
        }

        /// <summary>
        /// 打印应收对账单
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="restaurantIds"></param>
        /// <returns></returns>
        public async Task<IActionResult> ExportExcelReceivableStatement(DateTime? beginTime, DateTime? endTime, string restaurantIds = "")
        {
            var list = await _financeReportAppService.GetReceivableStatementListAsync(beginTime, endTime, restaurantIds);


            return DoExcel<ReceivableStatementDetailDto>(list, _env);
        }
        #endregion


        public async Task<IActionResult> SelectRestaurant(int id = 1, string keyword = "")
        {
            var lines = await _distributionLineAppService.GetAllLineAsync();
            ViewBag.Line = lines.Select(o => new SelectListItem() { Value = o.Id.ToString(), Text = o.LineName });

            var restaurant = await _restaurantAppService.GetAllPagedListAsync(id, 15, "", keyword, null, null);

            return View(restaurant);
        }

        public async Task<IActionResult> SelectRestaurantData(Guid? LineId, int id = 1, string keyword = "")
        {
            var restaurant = await _restaurantAppService.GetAllPagedListAsync(id, 15, "", keyword, null, LineId);
            return PartialView("SelectRestaurantPartial", restaurant);
        }


        /// <summary>
        /// 每日收支汇总
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> DailyIncomeAndExpenses(int pageIdx, int size, DateTime? beginTime, DateTime? endTime)
        {
            if (pageIdx == 0)
                pageIdx = 1;
            if (size == 0)
                size = 20;
            if (Request.IsAjaxRequest())
            {
                var model = await _financeReportAppService.GetDailyIncomeAndExpensesAsync(pageIdx, size, beginTime, endTime);
                return PartialView("DailyIncomeAndExpensesPartial", model);
            }
            return View();
        }

        /// <summary>
        /// 每日收支汇总导出
        /// </summary>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public async Task<IActionResult> ExportExcelDailyIncomeAndExpenses(DateTime? beginTime, DateTime? endTime)
        {
            var list = await _financeReportAppService.GetDailyIncomeAndExpensesAllListAsync(beginTime, endTime);
            return DoExcel<DailyIncomeAndExpensesDto>(list, _env);
        }
    }
}